﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Util;

namespace NTTDATA.BEENChina.ResultCertification.Split
{
    public class SplitAddressResult 
    {
        private static readonly string[] SplitCharacter = new string[] { " " };

        private static readonly string[] NumberChar = new[] { "丁目", "番地", "号", "号室", "階" };

        public static string GetMansionResult(string numberAndMansionResult)
        {
            List<string> splitReuslt = SplitNumberAndMansionResult(numberAndMansionResult).ToList();
 

            if (  splitReuslt.Count() > 1)
            {
                var number = splitReuslt.Skip(1);
                return String.Join(string.Empty, number);
            }
            else if (  splitReuslt.Count() == 1)
            {
                if (!IsNumber(splitReuslt[0]))
                {
                    return splitReuslt[0];
                }
            }

            return string.Empty; 
        }

        public static string GetNumberResult(string numberAndMansionResult)
        {
            IEnumerable<string> splitReuslt = SplitNumberAndMansionResult(numberAndMansionResult);
            
            var number = splitReuslt.TakeWhile(IsMansionResult).ToList();

            return  String.Join(string.Empty, number);
        }

        public static bool CheckNumber(string numberAndMansionResult)
        {
            IEnumerable<string> splitReuslt = SplitNumberAndMansionResult(numberAndMansionResult);

            var numbers = splitReuslt.TakeWhile(IsMansionResult).ToList();

            var numberResult = numbers.Select(t => NumberChar.Aggregate(t, (current, numberChar) => current.Replace(numberChar, string.Empty))).ToList();


            var notNumbers = numberResult.Where(t => !IsNumber(t)).ToList();
            return notNumbers.Any();
        }

        public static string GetCity(string result )
        {
            int townIndex = result.LastIndexOf('町');
            if (townIndex > -1)
            {
                if (townIndex == result.Length - 1)
                    return result;
            }
        

            int cityIndex = result.LastIndexOf('市');

            if (cityIndex > -1)
            {
                return result.Substring(0, cityIndex + 1);
            }
            return result;
        }

        public static string GetDistrict(string result)
        {
            int cityIndex = result.LastIndexOf('市');
            int districtIndex = result.LastIndexOf('区');

            if (cityIndex > -1 && districtIndex > -1)
            {
                if (districtIndex > cityIndex)
                {
                    return result.Substring(cityIndex + 1, districtIndex - cityIndex);
                }
                else
                {
                    return string.Empty;
                }
            }
            else if ( districtIndex > -1)
            {
                return result.Substring(0, districtIndex + 1);
            }
            return string.Empty;
        }

        private static bool IsMansionResult(string result,int index)
        {
            bool bCheck = false;

            //全是数字
            bCheck = index.Equals(0) && IsNumber(result);
                

            if (bCheck)
            {
                //全是数字
                return true;
            }
            return false;
             
        }

        private static IEnumerable<string> SplitNumberAndMansionResult(string entryResult)
        {

            List<string> splitReuslt = entryResult.Split(SplitCharacter, StringSplitOptions.None).ToList();

            return splitReuslt.Where(t => !string.IsNullOrEmpty(t)).Where(t => !string.IsNullOrWhiteSpace(t)).Select(t => t.Trim()).ToList();
        }
        

        private static bool IsNumber(string result)
        {
            bool bCheck = result.ToCharArray().All(t =>
            {
                bool b = false;
                if (t.Equals('-'))
                    return true;
                b = StrHelper.IsNumeric(
                    t.ToString(CultureInfo.InvariantCulture));
                return b;
            });
            return bCheck;
        }

        /// <summary>
        /// 番号与ms未加空格
        /// </summary>
        /// <param name="numberMansionResult"></param> 
        /// <returns></returns>
        public static bool CheckSeparator(string numberMansionResult)
        {
            bool bresult = false;
            //去掉前后空格
            numberMansionResult = numberMansionResult.Trim();
            //番号与ms未加空格

            //数字＋階］也属于番号记载，与后面的记载没加空格时要抽出
            //４階西武ハウス株式会社

            foreach (var numberChar in NumberChar)
            {


                int numberIndex = numberMansionResult.IndexOf(numberChar, System.StringComparison.Ordinal);

                if (numberIndex > 0 && numberIndex + 1 < numberMansionResult.Length)
                {
                    string nextChar = numberMansionResult.Substring(numberIndex  + 1 ,   1);
                    string prePart = numberMansionResult.Substring(0, numberIndex );

                    if (IsNumber(prePart) && !string.IsNullOrWhiteSpace(nextChar))
                    {
                        bresult = true;
                        break;
                      
                    }
                }
            }
            return bresult;
        }
    }
}
